package components.plugins.frames
{
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import UIUtils.uif_utilBitmap;
	
	import classes.clt_events;
	
	import components.clt_arrowB;
	import components.tools.clt_selectLayer;
	
	import data.frames.cd_frames;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	
	public class clt_framesLayer extends clt_selectLayer {
		public static const FRAMENAME			: String = 'frame';
		public static const FRAMESELECT			: String = 'FRAMESELECT';
		public static const LOADFRAME			: String = 'LOADFRAME';
		private var _delButton					: clt_arrowB;
		
		public function clt_framesLayer(a_clt: clt_clothingBase) {
			var rect: Rectangle = a_clt.tools.toolLayer.getInner();
			super(a_clt.tools.toolLayer, cd_frames, clt_servers.FRAME_URL + 'jpg_preview/i',
				FRAMESELECT, rect.x, rect.y, rect.width - 30, rect.height);
		}
		
		override protected function createComponentsA(a_data: Object, previewURL: String, selectEvent: String): void {
			super.createComponentsA(a_data, previewURL, selectEvent);
			var rect: Rectangle = getInner();
			rect.left 	+= rect.width - 150;
			rect.top	 = rect.height - 33;
			_delButton = addButton('Удалить элемент', function(): void {
				workspace.dispatchEvent(new ObjectEvent(clt_events.SETELEMENT, {
					name	: FRAMENAME,
					bmp		: null,
					data	: {}
				}));
			}, rect.clone());
			rect.x -= 150;
			addButton('Загрузить элемент', function(): void {
				clt.loadImage(LOADFRAME, 
					[new FileFilter('PNG формат', '*.png'),
					 new FileFilter('GIF формат', '*.gif')]);
			}, rect.clone());
			refreshButtonDisable();
		}
		
		private function refreshButtonDisable(): void {
			_delButton.disable = clt.resultLayer.display.getElem(FRAMENAME) == null;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(FRAMESELECT, doFrameSelect, clt.workspace); 
			addEvent(LOADFRAME, doFrameLoad, clt.workspace); 
			addEvent(clt_events.UPDATEELEMENT, doElementUpdate, clt.workspace); 
		}
		
		private function doElementUpdate(e: ObjectEvent): void {
			if (e.object.name == FRAMENAME) 
				_delButton.disable = e.object.data == null;
		}
		
		public function execute(a_params: Object): void {
			clt.workspace.dispatchEvent(new ObjectEvent(clt_events.SHOWTOOLS, this));
		}
		
		protected function doFrameSelect(e: ObjectEvent): void {
			clt.addElementJPGTmpl(FRAMENAME, clt_servers.FRAME_URL + 'jpg/' + e.object.id);
		}
		
		protected function doFrameLoad(e: ObjectEvent): void {
			workspace.dispatchEvent(new ObjectEvent(clt_events.SETELEMENT, {
				name	: FRAMENAME,
				bmp		: new Bitmap(uif_utilBitmap.copyBitmapData(e.object as DisplayObject)),
				data	: {}
			}));
		}
		
		override protected function getSelectRect(): Rectangle {
			return getInner(0, 0, 0, -33);
		}		
	}
}